package com.beiben.trialproduce.part.controller;

import java.util.List;

import com.beiben.experiment.repair.domain.Repair;
import com.beiben.trialproduce.samplecar.domain.SampleCar;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.beiben.trialproduce.part.domain.Part;
import com.beiben.trialproduce.part.service.IPartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 零件Controller
 * 
 * @author 熊国强
 * @date 2020-11-25
 */
@RestController
@RequestMapping("/trialproduce/part")
public class PartController extends BaseController
{
    @Autowired
    private IPartService partService;

    /**
     * 查询零件列表
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:list')")
    @GetMapping("/list")
    public TableDataInfo list(Part part)
    {
        startPage();
        List<Part> list = partService.selectPartList(part);
        return getDataTable(list);
    }

    /**
     * 导出零件列表
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:export')")
    @Log(title = "零件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Part part)
    {
        return partService.downLoadPartExcel(part);
    }

    /**
     * 获取零件详细信息
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:query')")
    @GetMapping(value = "/{partId}")
    public AjaxResult getInfo(@PathVariable("partId") Long partId)
    {
        return AjaxResult.success(partService.selectPartById(partId));
    }

    /**
     * 新增零件
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:add')")
    @Log(title = "零件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Part part)
    {
        return toAjax(partService.insertPart(part));
    }

    /**
     * 修改零件
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:edit')")
    @Log(title = "零件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Part part)
    {
        return toAjax(partService.updatePart(part));
    }

    /**
     * 删除零件
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:part:remove')")
    @Log(title = "零件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{partIds}")
    public AjaxResult remove(@PathVariable Long[] partIds)
    {
        return toAjax(partService.deletePartByIds(partIds));
    }

    /**
     * 导入零件信息
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Part> util = new ExcelUtil<Part>(Part.class);
        return util.importTemplateExcel("零件数据");
    }

    @Log(title = "零件导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('trialproduce:part:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Part> util = new ExcelUtil<Part>(Part.class);
        List<Part> partList = util.importExcel(file.getInputStream());
        String message = partService.importPart(partList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 修改样车 使用状态
     */
    @PostMapping("/changeState")
    public AjaxResult changeState(@RequestBody Part part)
    {
        return toAjax(partService.updatePartState(part));
    }
}
